//
//  CMDBLoginManager.swift
//  CMDB
//
//  Created by 王磊 on 17/4/5.
//  Copyright © 2017年 CMDB. All rights reserved.
//

import UIKit

class CMDBLoginManager: NSObject {
    
    static let getInstance: CMDBLoginManager = CMDBLoginManager()
    
    private override init() {
        
    }
}

// MARK: 登陆
/*
 let loginParam = CMDBLoginParam()
 
 do {
 
 try CMDBLoginManager.getInstance.login(loginParam, handler: { (account) in
 
 
 }, error: { (rescode) in
 
 }, faliure: {
 
 })
 
 } catch CMDBUserControllerExceptionType.ExceptionCode(code: CMDBUserControllerExceptionCodeType.PhoneInvalid) {
 
 
 CMDBShowInfoManager.getInstance.showInfoWithStatus("无效手机号!")
 
 
 } catch CMDBUserControllerExceptionType.ExceptionCode(code: CMDBUserControllerExceptionCodeType.PasswordInvalid) {
 
 
 CMDBShowInfoManager.getInstance.showInfoWithStatus("密码小于6位!")
 
 } catch {
 
 printLog(error)
 
 }
 */

extension CMDBLoginManager: CMDBLoginManagerDelegate {
    
    /**********************
     * account 数据缓存存储到 CMDBApplicationManager这个单例类里
     *
     *********************/
    func login(loginParam: CMDBLoginParam, handler: CMDBAccountHandler,error: CMDBHttpErrorHandler, faliure: CMDBHttpFailureHandler) throws {
        
        guard loginParam.phone.cmdb_length != 0 else {
            
            throw CMDBUserControllerExceptionType.ExceptionCode(code: CMDBUserControllerExceptionCodeType.PhoneIsEmpty)
            
        }
        
        guard loginParam.phone.validPhoneNumber() else {
            
            throw CMDBUserControllerExceptionType.ExceptionCode(code: CMDBUserControllerExceptionCodeType.PhoneInvalid)
        }
        
        guard loginParam.password.cmdb_length != 0 else {
            
            throw CMDBUserControllerExceptionType.ExceptionCode(code: CMDBUserControllerExceptionCodeType.PasswordIsEmpty)
            
        }
        
        guard loginParam.password.cmdb_length >= Password_Min_Length else {
            // 可以在异常中 toast
            throw CMDBUserControllerExceptionType.ExceptionCode(code: CMDBUserControllerExceptionCodeType.PasswordInvalid)
        }
        let param = loginParam.keyValues
        
        CMDBHttpManager.getInstance.request(loginParam.URLString(), param: param, success: { (sysTime, data, page) in
            
            let account: CMDBAccountBean = CMDBAccountBean.objectWithKeyValues(data as! [String: AnyObject]) as! CMDBAccountBean
            
            CMDBApplicationManager.getInstance.saveAccount(account)
            
            handler(account: account)
            
            }, error: error, failure: faliure)
    }
    
    func logout() {
        
        CMDBApplicationManager.getInstance.clearAccount()
        
        let logoutParam = CMDBLogoutParam()
        
        CMDBHttpManager.getInstance.request(logoutParam.URLString(), param: nil, success: { (sysTime, data, page) in
            
            }, error: { (rescode) in
                
        }) {
            
            
        }
    }
}